package com.flower8.controller;

import com.flower8.dto.SysMsgDto;
import com.flower8.model.Bank;
import com.flower8.model.SysMsg;
import com.flower8.service.BankService;
import com.flower8.service.MessageService;
import com.flower8.utils.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: tanglin
 * @Date: 2019/12/25 14:35
 * @Version: 1.0
 */
@RestController
@RequestMapping("/msg")
public class MessageController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(MessageController.class);

    @Autowired
    private MessageService messageService;

    /**
     * 查询消息列表
     * @param request
     * @return
     */
    @RequestMapping("/getMsgList")
    public Object getMsgList(HttpServletRequest request){
        logBefore(log, "++++++++++ 查询消息列表 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<SysMsgDto> bankList =  messageService.getMsgList(pd);
            int total = messageService.getMsgListCount(pd);
            res.put("data", bankList);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return res;
    }

    /**
     * 新增消息
     * @param request
     * @return
     */
    @RequestMapping("/addMsg")
    public Object addMsg(HttpServletRequest request){
        logBefore(log, "++++++++++ 新增消息 ++++++++++");
        PageData pd = new PageData(request);

        Map res = new HashMap();
        try {
            messageService.addMsg(pd);
            logAfter(log);
            return responseResultForSuccess(null, "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("操作失败");
        }
    }

    /**
     *  删除消息
     * @param request
     * @return
     */
    @RequestMapping("/delMsg")
    public Object delMsg(HttpServletRequest request){
        logBefore(log, "++++++++++ 删除消息 ++++++++++");
        PageData pd = new PageData(request);

        Map res = new HashMap();
        try {
            messageService.delMsg(pd);
            logAfter(log);
            return responseResultForSuccess(null, "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("删除失败");
        }
    }

    /**
     * 批量删除消息
     * @param request
     * @return
     */
    @RequestMapping("/delBatchMsg")
    public Object delBatchMsg(HttpServletRequest request){
        logBefore(log, "++++++++++ 批量删除消息 ++++++++++");
        PageData pd = new PageData(request);
        String ids = pd.getString("ids");
        try {
            messageService.delBatchMsg(ids);
            logAfter(log);
            return responseResultForSuccess(null, "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("删除失败");
        }
    }
}
